import 'package:flutter/services.dart';

import 'flutter_video_info_platform_interface.dart';

/// An implementation of [FlutterVideoInfoPlatform] that uses method channels.
class MethodChannelFlutterVideoInfo extends FlutterVideoInfoPlatform {
  /// The method channel used to interact with the native platform.
  final methodChannel = const MethodChannel('flutter_video_info');

  final eventChannel = const EventChannel('flutter_video_info_stream');

  static const getVideoCodecMethod = 'getVideoCodec';

  static const compressVideoMethod = 'compressVideo';

  static const cancelCompressingMethod = 'cancelCompressing';

  @override
  Future<String?> getVideoCodec(String videoPath) async {
    final codecType = await methodChannel.invokeMethod<String>(
        getVideoCodecMethod, videoPath);
    return codecType;
  }

  @override
  Future<String?> compressVideo(String videoPath) async {
    final compressedVideoPath = await methodChannel.invokeMethod<String>(
        compressVideoMethod, videoPath);
    return compressedVideoPath;
  }

  @override
  Stream<double> get progressStream => eventChannel
      .receiveBroadcastStream()
      .distinct()
      .map((progress) => progress as double);

  @override
  Future<void> cancelCompressing() async {
    await methodChannel.invokeMethod(cancelCompressingMethod);
  }
}
